import { useState, useCallback } from 'react';

function useRowClickSelection(rowKey) {
    const [selectedRowKeys, setSelectedRowKeys] = useState([]);
    const onRowClick = useCallback((e, record) => {
        e.stopPropagation();
        let newSelectedRowKeys = [...selectedRowKeys];
        const matchedIndex = newSelectedRowKeys.findIndex(v => v === record[rowKey]);
        if (matchedIndex > -1) {
            // * 移除
            newSelectedRowKeys.splice(matchedIndex, 1);
        } else {
            // * 新增
            newSelectedRowKeys.push(record[rowKey]);
        }
        console.log('Next selectedRowKeys is', newSelectedRowKeys)
        setSelectedRowKeys(newSelectedRowKeys);
    }, [selectedRowKeys]);
    const handleSelectChange = useCallback((selectedRowKeys) => {
        setSelectedRowKeys(selectedRowKeys);
    }, []);
    return { onRowClick, rowSelection: { selectedRowKeys, onChange: handleSelectChange }, setSelectedRowKeys };
}
export default useRowClickSelection;